<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
?>
<?php
class MovieController extends Zend_Controller_Action{
	/**
	 * catch-all do re-direct ( no exceptions )
	 */
    public function indexAction(){
		$this->_forward( 'notfound', 'error' );
    }

	/**
	 * Show movie as a JSON oject.
	 * This Callback is used in XmlHttpRequests
	 *
	 *	@param array $value Allow the return of extra data then the default ( wich is all the info about this movie )
	 *	@return void
	 */
    public function jsonAction( array $value = array() ){
		$request 	= $this->getRequest();
		$movie   	= new Default_Model_Movie( $request->id );
		$annotation = new Default_Model_Annotation( array('movie_id' => $movie->id ));

		$annotation->setLimit( 0 );
		//$channel->setMovie( $movie );

		if( $request->action == 'annotate' ){
			$annotation->userId = $this->_helper->user();
		}

		$annotations = $annotation->fetchAll();

		foreach( $annotations as $i => $annotation ){
			$annotations[$i] = $annotation->toArray();
		}

		$channels = $movie->getChannels();

		foreach( $channels as $i => $channel ){
			$channels[$i] = $channel->toArray();
		}

		$info = $movie->getInfo();
		$movie = array_merge( $movie->toArray(), $info->toArray() );

		$response = array(
            'success'       => true,
            'movies'        => array( $movie ),
            'channels'      => $channels,
            'annotations'   => $annotations
        );

        $response = array_merge( $value, $response );

        $this->_helper->json( $response );
    }

	public function listAction(){
		$request = $this->getRequest();

		$movie = new Default_Model_Movie();

		$movie->setOffset( $request->offset );
		$movie->setLimit( $request->limit );

		if( isset( $request->channel ) ){
			$movie->setChannel( new Default_Model_Channel( $request->channel ) );
		}

		$this->view->movies = $movie->fetchAll();
		$this->view->movie  = $movie;
	}

	public function annotateAction(){
		if( ! Zend_Auth::getInstance()->hasIdentity() ){
			$this->_forward( 'login', 'user' );
		}

        $request = $this->getRequest();

		if( null === $request->id || ! is_numeric( $request->id ) ){
			$this->_forward( 'notfound', 'error' );
			return;
		}
		else if( $request->isXmlHttpRequest() || $request->format == 'json' ){
			$this->_forward( 'json' );
        }

		$movie   = new Default_Model_Movie( $request->id );
		$channel = new Default_Model_Channel( $request->channel );
		$user    = $this->_helper->user();

		$annotation = new Default_Model_Annotation(array(
			'movie_id'	=> $movie->id,
			'user_id'	=> $user->id
		));

		$annotation->setOffset( $request->ofset );

		// set all the stuff this view needs
        $this->view->pageTitle = sprintf( 'Playing movie "%s"', $movie->title );

        $this->view->annotations = (array) $annotation->fetchAll();
		$this->view->annotation  = $annotation;
        $this->view->movie       = $movie;
        $this->view->channel     = $channel;
        $this->view->channels    = (array) $movie->getChannels();
        $this->view->url         = '/movie/' . $this->getRequest()->action . '/' . $movie->id;
	}

    public function playAction(){
        $request = $this->getRequest();

		if( null === $request->id || ! is_numeric( $request->id ) ){
			$this->_forward( 'notfound', 'error' );
			return;
		}
		else if( $request->isXmlHttpRequest() || $request->format == 'json' ){
			$this->_forward( 'json' );
        }

		$movie   = new Default_Model_Movie( $request->id );
		$channel = new Default_Model_Channel( $request->channel );

		$annotation = new Default_Model_Annotation(array('movie_id'	=> $movie->id ));
		$annotation->setOffset( $request->ofset );

		// set all the stuff this view needs
        $this->view->pageTitle = sprintf( 'Playing movie "%s"', $movie->title );

        $this->view->annotations = $annotation->fetchAll();
		$this->view->annotation = $annotation;
        $this->view->movie      = $movie;
        $this->view->channel    = $channel;
        $this->view->channels   = $movie->getChannels();
        $this->view->url        = '/movie/' . $this->getRequest()->action . '/' . $movie->id;
   }
}
